﻿using Assignment_3.Classes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Assignment_3.Feature___Booking
{
    public partial class RetrieveBooking : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                bookingDList.DataSource = DatabaseManager.RetrieveAllBooking();
                bookingDList.DataBind();
            }
            panelMsg.Controls.Clear();
            foreach (DataListItem di in bookingDList.Items)
            {
                string status = ((Label)di.FindControl("Room_StatusLabel")).Text;
                if (status.Equals("Reserved", StringComparison.OrdinalIgnoreCase) || status.Equals("Vacant", StringComparison.OrdinalIgnoreCase))
                {
                    ((Button)di.FindControl("btnCheckIn")).Visible = true;
                }
            }
        }

        protected void bookingDList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                e.Item.Attributes.Add("onmouseover", "this.oldClass = this.className; this.className = 'EntryLineHover'");
                e.Item.Attributes.Add("onmouseout", "this.className = this.oldClass;");
                e.Item.Attributes.Add("onclick", this.Page.ClientScript.GetPostBackEventReference(e.Item.Controls[1], string.Empty));
            }
        }

        protected void bookingDList_ItemCommand(object source, DataListCommandEventArgs e)
        {
            int bookingIDCheckIn = Convert.ToInt32(((Label)e.Item.FindControl("Booking_IDLabel")).Text);
            int successCheckIn = DatabaseManager.CheckIn(bookingIDCheckIn);

            if (successCheckIn == 1)
            {
                Literal litPanelMsg = new Literal();
                litPanelMsg.Text = "<h4 class='alert_success'>Success: Booking No. [" + bookingIDCheckIn + "] Checked-in.</h4>";
                panelMsg.Controls.Add(litPanelMsg);

                foreach (DataListItem di in bookingDList.Items)
                {
                    string status = ((Label)di.FindControl("Room_StatusLabel")).Text;
                    if (status.Equals("Reserved", StringComparison.OrdinalIgnoreCase) || status.Equals("Vacant", StringComparison.OrdinalIgnoreCase))
                    {
                        ((Button)di.FindControl("btnCheckIn")).Visible = true;
                    }
                }
            }

            bookingDList.DataSource = DatabaseManager.RetrieveAllBooking();
            bookingDList.DataBind();
        }

        protected void btnRetrieve_Click(object sender, EventArgs e)
        {
            int bookingIDRetrieve = Convert.ToInt32(tbxRetrieve.Text);
            DataSet ds = DatabaseManager.RetrieveBookingByID(bookingIDRetrieve);

            if (ds.Tables[0].Rows.Count == 0)
            {
                bookingDList.Visible = false;

                panelMsg.Controls.Clear();
                Literal litMsg = new Literal();
                litMsg.Text = "<h4 class='alert_error'>Error: No such booking exists. &nbsp;&nbsp; - Please go back or click 'Show All' below.</h4>";
                panelMsg.Controls.Add(litMsg);
            }
            else
            {
                bookingDList.Visible = true;
                bookingDList.DataSource = ds;
                bookingDList.DataBind();

                panelMsg.Controls.Clear();
                Literal litMsg = new Literal();
                litMsg.Text = "<h4 class='alert_success'>Success: Booking No. [" + bookingIDRetrieve + "] displayed.</h4>";
                panelMsg.Controls.Add(litMsg);

                foreach (DataListItem di in bookingDList.Items)
                {
                    string status = ((Label)di.FindControl("Room_StatusLabel")).Text;
                    if (status.Equals("Reserved", StringComparison.OrdinalIgnoreCase) || status.Equals("Vacant", StringComparison.OrdinalIgnoreCase))
                    {
                        ((Button)di.FindControl("btnCheckIn")).Visible = true;
                    }
                }
            }
        }

        protected void btnShowAll_Click(object sender, EventArgs e)
        {
            tbxRetrieve.Text = String.Empty;
            bookingDList.DataSource = DatabaseManager.RetrieveAllBooking();
            bookingDList.DataBind();
            bookingDList.Visible = true;

            foreach (DataListItem di in bookingDList.Items)
            {
                string status = ((Label)di.FindControl("Room_StatusLabel")).Text;
                if (status.Equals("Reserved", StringComparison.OrdinalIgnoreCase) || status.Equals("Vacant", StringComparison.OrdinalIgnoreCase))
                {
                    ((Button)di.FindControl("btnCheckIn")).Visible = true;
                }
            }

            panelMsg.Controls.Clear();
            Literal litMsg = new Literal();
            litMsg.Text = "<h4 class='alert_success'>All booking records displayed.</h4>";
            panelMsg.Controls.Add(litMsg);
        }
    }
}